package com.qyhl.webtv.module_news.luckydraw;

import android.Manifest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.SectionBean;
import com.qyhl.webtv.commonlib.utils.MShareBoard;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.commonlib.utils.inter.HomeActivityInterface;
import com.qyhl.webtv.module_news.luckydraw.fragment.LuckDrawFragment;
import com.qyhl.webtv.module_news.news.newslist.NewListFragment;
import com.weavey.loading.lib.LoadingLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by Administrator on 2018/6/12.
 */

public class LuckDrawHomeFragment extends BaseFragment implements LuckDrawContract.LuckDrawView {
    @BindView(R2.id.back)
    ImageView back;
    @BindView(R2.id.share_btn)
    ImageView shareBtn;
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager mViewPager;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    private View view;
    @BindView(R2.id.luck_title)
    RelativeLayout luck_title;

    //标题
    private String title;
    private String catalogId;

    private LuckDrawPresenter mPresenter;
    private List<SectionBean> mList;
    private List<String> titleList;
    private StringBuilder sections;

    //PageAdapter
    private LuckDrawPageAdapter mViewPageadapter;
    // Frgaments
    private List<Fragment> mFragmentList;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setCatalogId(String catalogId) {
        this.catalogId = catalogId;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.news_fragment_home_luck_draw, null);
    }

    @Override
    protected void create() {
        mPresenter = new LuckDrawPresenter(this);

        initView();
    }


    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .titleBar(luck_title)
                .init();
    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getSections(catalogId,"2");
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    public void setHomeActivityInterface(HomeActivityInterface homeActivityInterface) {
        this.homeActivityInterface = homeActivityInterface;
    }

    HomeActivityInterface homeActivityInterface;

    public static LuckDrawHomeFragment newInstance(String catalogId, String title, HomeActivityInterface homeActivityInterface) {
        LuckDrawHomeFragment fragment = new LuckDrawHomeFragment();
        fragment.setTitle(title);
        fragment.setCatalogId(catalogId);
        fragment.setHomeActivityInterface(homeActivityInterface);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }


    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        mList = new ArrayList<>();
        mTitle.setText(StringUtils.isEmpty(title) ? "开票有喜" : title);
    }

    /**
     * 设置监听
     */
    private void setListeners() {
        //重载
        loadMask.setOnReloadListener(view -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getSections(catalogId,"2");
        });


        //分享
        shareBtn.setOnClickListener(view -> MPermissionUtils.requestPermissionsResult(getActivity(), 1, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new MPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                if (StringUtils.isNotEmpty(catalogId)) {
                    MShareBoard shareBoard = new MShareBoard(getActivity(),catalogId,StringUtils.isEmpty(title) ? "开票有喜" : title,"","", CommonUtils.getInstance().getBannerId(),18);
                    shareBoard.showPopupWindow();
                } else {
                    Toasty.normal(getContext(), "请加载完成后再分享！").show();
                }
            }

            @Override
            public void onPermissionDenied() {
                MPermissionUtils.showTipsDialog(getActivity());
            }
        }));

    }



    @Override
    public void setDatas(List<SectionBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        mTabLayout.setVisibility(View.VISIBLE);
        mList = beanList;
        sections = new StringBuilder();
        mFragmentList = new ArrayList<>();
        titleList = new ArrayList<>();
        titleList.add("首页");

        for (int i = 0; i < mList.size(); i++) {
            mFragmentList.add(NewListFragment.newInstance(mList.get(i).getSectionId() + ""));
            titleList.add(mList.get(i).getSectionName());
            if (i == 0) {
                sections.append(mList.get(i).getSectionId() + "");
            } else {
                sections.append("," + mList.get(i).getSectionId());
            }
        }
        mFragmentList.add(0, LuckDrawFragment.newInstance(sections.toString(), catalogId, true,homeActivityInterface));
        mViewPageadapter = new LuckDrawPageAdapter(getChildFragmentManager(), mFragmentList);
        mViewPageadapter.setData(titleList);
        mViewPager.setAdapter(mViewPageadapter);
        try {
            Class<? extends SlidingTabLayout> aClass = mTabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(mTabLayout, StringUtils.dip2px(getContext(), 15));
        } catch (Exception e) {
            e.printStackTrace();
        }

        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        mTabLayout.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        mTabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
        mViewPageadapter.notifyDataSetChanged();

    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setErrorText(msg);
    }

    @Override
    public void setNetError(String msg) {
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setNoNetworkText(msg);
    }

    @Override
    public void setEmpty() {
        mTabLayout.setVisibility(View.GONE);
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        mFragmentList = new ArrayList<>();
        titleList = new ArrayList<>();
        titleList.add("首页");
        mFragmentList.add(LuckDrawFragment.newInstance("", catalogId, true,homeActivityInterface));

        mViewPageadapter = new LuckDrawPageAdapter(getChildFragmentManager(), mFragmentList);
        mViewPageadapter.setData(titleList);
        mViewPager.setAdapter(mViewPageadapter);
        try {
            Class<? extends SlidingTabLayout> aClass = mTabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(mTabLayout, StringUtils.dip2px(getContext(), 15));
        } catch (Exception e) {
            e.printStackTrace();
        }

        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        mTabLayout.setTextUnselectColor(HomeThemeColorUtil.getNormalColor());
        mTabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
        mViewPageadapter.notifyDataSetChanged();
    }

}